<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>RegExp</title>
</head>
<body>
<div class="samurai ninja"></div>
<div class="kiki ninja"></div>
<script>
    function findElementsByClassName(className, tag) {
        const elements = document.getElementsByTagName(tag || '*');
        const regex = new RegExp('(^|\\s)' + className + '(\\s|$)');
        return [...elements].filter(item => regex.test(item.className))
    }

    console.log(findElementsByClassName('ninja', 'div'))
</script>
</body>
</html>
